# agents/factor_agents/hysilens_agent.py
from .base_factor_agent import BaseFactorAgent
from game_engine.game_state import FactorDevelopment, GameWorld
import numpy as np

class HysilensAgent(BaseFactorAgent):
    """海瑟音智能体 - 持续伤害特化 [火种：海洋]"""
    
    def __init__(self, factor: FactorDevelopment, game_world: GameWorld):
        super().__init__(factor, game_world)
        self.personality_traits.update({
            "aggression": 0.6,      # 中等攻击性
            "caution": 0.7,         # 较高谨慎性
            "patience": 0.9,        # 极高耐心
            "tidal_focus": 0.85     # 潮汐专注
        })
        self.dot_effects = {}  # 持续伤害效果
        self.tidal_phase = 0  # 潮汐相位
        self.last_tide_change = 0
    
    def make_decision(self, current_time: int) -> str:
        """海瑟音的决策逻辑"""
        situation = self.evaluate_situation()
        
        # 潮汐相位管理
        if current_time - self.last_tide_change > 25:
            self.tidal_phase = (self.tidal_phase + 1) % 4
            self.last_tide_change = current_time
        
        # 应用持续伤害
        active_targets = [target for target, dot in self.dot_effects.items() 
                         if dot["duration"] > 0]
        
        if active_targets and self.tidal_phase == 3:  # 高潮期
            return "amplify_dots"
        
        # 寻找新目标施加DOT
        if situation["suitable_targets"] and len(active_targets) < 3:
            target = situation["suitable_targets"][0]
            dot_type = self._choose_dot_type(target)
            return f"apply_{dot_type}:{target.name}"
        
        # 维持现有DOT
        if active_targets:
            weakest_dot = min(active_targets, 
                            key=lambda x: self.dot_effects[x]["duration"])
            return f"refresh_dot:{weakest_dot}"
        
        # 准备阶段
        return "prepare_tides"
    
    def evaluate_situation(self) -> Dict:
        """评估当前局势"""
        nearby_factors = self.get_nearby_factors()
        
        suitable_targets = []
        for factor in nearby_factors:
            if (self._is_potential_threat(factor) and 
                factor.attributes['health'] > 30):  # 血量足够承受DOT
                suitable_targets.append(factor)
        
        return {
            "suitable_targets": suitable_targets,
            "active_dots": len([dot for dot in self.dot_effects.values() if dot["duration"] > 0]),
            "tidal_phase": self.tidal_phase,
            "high_tide_soon": (25 - (self.game_world.current_time - self.last_tide_change)) < 5
        }
    
    def _choose_dot_type(self, target: FactorDevelopment) -> str:
        """选择持续伤害类型"""
        if target.attributes['defense'] > 25:
            return "armor_penetration_dot"  # 破甲DOT
        elif target.attributes['mobility'] > 20:
            return "slow_dot"  # 减速DOT
        else:
            return "standard_dot"  # 标准DOT
    
    def update_dots(self):
        """更新持续伤害效果"""
        expired = []
        for target_name, dot_info in self.dot_effects.items():
            dot_info["duration"] -= 1
            if dot_info["duration"] <= 0:
                expired.append(target_name)
        
        for target_name in expired:
            del self.dot_effects[target_name]